package com.gwtintegration.smartrf.builder;

import com.google.gwt.core.ext.typeinfo.JClassType;
import com.google.gwt.core.ext.typeinfo.JMethod;
import com.google.gwt.user.rebind.SourceWriter;

public class PrimitivePrimaryKeyFieldConverterBuilder extends PrimitiveFieldConverterBuilder {

	protected PrimitivePrimaryKeyFieldConverterBuilder(JMethod getterMethod, ProxyConverterBuilder proxyConverterBuilder) {
		super(getterMethod, proxyConverterBuilder);
	}

	@Override
	public boolean buildEquals(SourceWriter sourceWriter) {

		JMethod getterMethod = getGetterMethod();
		JClassType returnType = getterMethod.getReturnType().isClassOrInterface();

		String proxyVarName = getVariableName();
		String recordVarName = proxyVarName + "Attr";
		String attrName = getAttributeName();

		extractPrimitiveRecordAttribute(sourceWriter, recordVarName, attrName, returnType);
		sourceWriter.println("%s %s = proxy.%s();", returnType.getName(), proxyVarName, getterMethod.getName());

		sourceWriter.println("if (%s == null) {", proxyVarName);
		sourceWriter.indent();
		sourceWriter.println("if (%s != null) {", recordVarName);
		sourceWriter.indentln("return false;");
		sourceWriter.println("}");
		sourceWriter.outdent();
		sourceWriter.println("} else if (!%s.equals(%s)) {", proxyVarName, recordVarName);
		sourceWriter.indentln("return false;");
		sourceWriter.println("}");

		return true;
	}
}
